<template>
  <div class="saleaman">
    <breadcrumb class="breadcrumb-container"></breadcrumb>
    <el-row>
      <el-form :model="searchForm" inline size="small">
        <el-form-item label="">
          <el-button size="small" icon="el-icon-plus" type="primary" @click="()=>{this.$router.push({name: 'add'})}">添加业务员</el-button>
        </el-form-item>
        <el-form-item label="业务员编号" prop="">
          <el-input placeholder="请输入业务编号" v-model="searchForm.serial_number"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="">
          <el-input placeholder="请输入姓名" v-model="searchForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="">
          <el-input placeholder="请输入电话号码" v-model="searchForm.contact_phone" :maxlength="11"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button icon="el-icon-search" size="small" type="success" @click="handleClickSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row>
      <el-table size="small" :data="data" :header-cell-style="{background:'#f2f2f2'}" class="mt20">
        <el-table-column align="center" label="业务员编号" prop="sale_num"></el-table-column>
        <el-table-column align="center" label="业务员姓名" prop="name"></el-table-column>
        <el-table-column align="center" label="联系电话" prop="contact_phone"></el-table-column>
        <el-table-column align="center" label="流水及返佣" prop="rebate"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row.sale_num)">修改</el-button>
            <el-button type="text" size="small" class="err" @click="del(scope.$index, scope.row.sale_num, scope.row.name)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :total="total" layout="total, prev, pager, next, jumper"  class="fr mt20" :current-page.sync="currentPage" @current-change="handleCurrentChange" :page-size="pageSize"></el-pagination>
    </el-row>
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb'
import { init, search, del, page } from '@/api/saleaman'

export default {
  name: 'mysaleaman',
  components: {
    Breadcrumb
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 1,
      total: 0,
      data: [],
      searchForm: {
        serial_number: '',
        name: '',
        contact_phone: ''
      }
    }
  },
  methods: {
    handleDocumentInit() {
      init().then(response => {
        this.data = response.data.result.data
        this.total = response.data.result.total
        this.pageSize = response.data.result.pagecount
      })
    },
    edit(id) {
      this.$router.push({ name: 'edit', query: { id: id }})
    },
    top(id) {
      this.$router.push({ name: 'userShop', params: { id: id }})
    },
    handleClickSearch() {
      search(this.searchForm).then(response => {
        this.data = response.data.result.data
        this.total = response.data.result.total
        this.pageSize = response.data.result.pagecount
        this.currentPage = response.data.result.current_page
      })
    },
    del(index, id, name) {
      const model = confirm('是否删除业务员-' + name)
      if (model) {
        del(id).then(response => {
          if (response.data.errorCode === 0) {
            this.$message.success('操作成功')
            if (this.data.length > 0) {
              this.data.splice(index, 1)
            } else {
              this.currentPage > 1 ? this.handleCurrentChange(this.currentPage - 1) : this.handleCurrentChange()
            }
          } else {
            this.$message.error('操作失败')
          }
        })
      }
    },
    handleCurrentChange(v) {
      page(v, this.searchForm).then(response => {
        this.data = response.data.result.data
        this.total = response.data.result.total
        this.currentPage = response.data.result.current_page
      })
    }
  },
  created() {
    this.handleDocumentInit()
  }
}
</script>